//
//  SEGTextCell.m
//  YYKitDemo
//
//  Created by 刘山 on 2017/11/7.
//  Copyright © 2017年 Sam. All rights reserved.
//

#import "SEGTextCell.h"

#if __has_include(<Commom/WBTextLinePositionModifier.h>)
#import <Commom/WBTextLinePositionModifier.h>
#else
#import "WBTextLinePositionModifier.h"
#endif

#define kSEGTopBottomPadding 15
#define kSEGTitleWidth LYScreenWidth() - kSEGTopBottomPadding
#define kSEGTitleHeight 44
#define kSEGContentWidth LYScreenWidth() - 25
#define kSEGContentFontSize 16

@implementation SEGDoorTextLayout
- (instancetype)initWithContent:(NSString*)content ShowType:(SEGEnumCellShowType)type activity:(NSString*)activity{
    if (self == [super init]){
        _content = content;
        _type = type;
    }
    if (type == SEGEnumShowType_NoticeArea) {
        [self layoutNotice:activity];
    }else if(type == SEGEnumShowType_ShowNotice){
        [self layoutNotice:@""];
    }else if(type == SEGEnumShowType_OpenDoor){
        [self setLayoutDoorOpen];
    }else{
        [self layout];
    }
    
    return self;
}
- (instancetype)initWithContent:(NSString*)content ShowType:(SEGEnumCellShowType)type{
    return [self initWithContent:content ShowType:type activity:@""];
}

-(void)layout{
    _contentHeight = 0;
    _contentLayout = nil;
    _height = 0;
    
    [self layoutContent:@""];
    
    if (_type ==SEGEnumShowType_Title) {
        _height = 44;
        return;
    }
    
    _height += 15;
    _height += _contentHeight;
    _height += 15;
    
    if (_type ==SEGEnumShowType_Fix) {
        _height += 35;
        _height += 30;
    }
}
-(void)layoutContent:(NSString*)activity{
    UIColor *textColor = kColor_1;
    textColor = (activity && [activity isEqualToString:@"1"]) ? kColor_T : kColor_1;
    NSMutableAttributedString *text = [SEGKitHelper seg_TextWithString:_content fontSize:16 textColor:textColor];
    if (text.length == 0) return;
    
    WBTextLinePositionModifier *modifier = [WBTextLinePositionModifier new];
    modifier.font = [UIFont fontWithName:@"Heiti SC" size:16];
    modifier.paddingTop = 0;
    modifier.paddingBottom = 0;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth() - 40, HUGE);
    container.linePositionModifier = modifier;
    
    _contentLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentLayout) return;
    _contentHeight = [modifier heightForLineCount:_contentLayout.rowCount];
}

-(void)layoutNotice:(NSString*)activity{
    _contentHeight = 0;
    _contentLayout = nil;
    _height = 44;
    [self layoutContent:activity];
}


- (instancetype)initWithData:(SEGNewMyDoorListModel*)data ShowType:(SEGEnumCellShowType)type{
    if (self == [super init]){
        _data = data;
        _type = type;
    }
    [self setLayoutDoorOpen];
    return self;
}
-(void)setLayoutDoorOpen{
    _height = 44 + 15;
}
@end

@implementation SEGTextCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.titleImageView];
    [self.contentView addSubview:self.submitButton];
    [self.contentView addSubview:self.switchButton];
    [self.contentView addSubview:self.doorView];
    return self;
}

- (void)setLayout:(SEGDoorTextLayout *)layout{
    
    if (layout.type == SEGEnumShowType_ShowNotice) {
        [self setLayoutNotice:layout];
        return;
    }
    
    if (layout.type == SEGEnumShowType_NoticeArea) {
        [self setLayoutNoticeChoose:layout];
        return;
    }
    
    if (layout.type == SEGEnumShowType_OpenDoor) {
        [self setLayoutDoorOpen:layout];
        return;
    }
    
    [self setLayoutText:layout];
}

- (void)setLayoutText:(SEGDoorTextLayout *)layout{
    self.titleImageView.hidden = layout.type == SEGEnumShowType_Title ? NO : YES;
    [self.titleImageView setFrame:CGRectMake(15, 15, self.titleImageView.width, self.titleImageView.height)];
    
    self.contentLabel.textLayout = layout.contentLayout;
    [self.contentLabel setFrame:CGRectMake(25, 15, LYScreenWidth() - 40, layout.contentHeight)];
    
    [self.submitButton setFrame:CGRectMake((LYScreenWidth() - 160) * 0.5, CGRectGetMaxY(self.contentLabel.frame) + 15, 160, 35)];
    self.submitButton.hidden = layout.type == SEGEnumShowType_Fix ? NO : YES;
    self.submitButton.layer.borderWidth = 0.5;
    self.submitButton.layer.borderColor = kColor_T.CGColor;
    self.submitButton.layer.cornerRadius = 4;
    self.submitButton.layer.masksToBounds = YES;
}

- (void)setLayoutNotice:(SEGDoorTextLayout *)layout{
    self.contentLabel.textLayout = layout.contentLayout;
    [self.contentLabel setFrame:CGRectMake(15, 15, LYScreenWidth() - 40, layout.contentHeight)];
    
    self.switchButton.hidden = NO;
    [self.switchButton setFrame:CGRectMake(LYScreenWidth() - self.switchButton.width - 15,
                                           (44 - self.switchButton.height) * 0.5,
                                           self.switchButton.height,
                                           self.switchButton.width)];
}

- (void)setLayoutNoticeChoose:(SEGDoorTextLayout *)layout{
    
    self.titleImageView.hidden = NO;
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_list_select")];
    [self.titleImageView setImage:[image imageByTintColor:kColor_T]];
    [self.titleImageView sizeToFit];
    [self.titleImageView setFrame:CGRectMake(LYScreenWidth() - self.titleImageView.width - 15,
                                             (44 - self.titleImageView.height) * 0.5,
                                             self.titleImageView.width,
                                             self.titleImageView.height)];
    
    self.contentLabel.textLayout = layout.contentLayout;
    [self.contentLabel setFrame:CGRectMake(15, 15, LYScreenWidth() - 40, layout.contentHeight)];
}

- (void)setLayoutDoorOpen:(SEGDoorTextLayout *)layout{
    self.doorView.hidden = NO;
    self.doorView.left = 15;
    self.doorView.top = 0;
    self.doorView.doorName = layout.data.name;
}

-(SEGSideslipView *)doorView{
    if (!_doorView) {
        _doorView = [[SEGSideslipView alloc]initWithFrame:CGRectZero];
        _doorView.hidden = YES;
        _doorView.delegate = self;
    }
    return  _doorView;
}

-(SEGLabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [SEGLabel new];
        _contentLabel.displaysAsynchronously = YES;
        _contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentTop;
        _contentLabel.ignoreCommonProperties = YES;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentLabel.fadeOnHighlight = NO;
        __weak typeof(self) _self = self;
        _contentLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(seg_CellClickAtIndexPath:)]) {
                [_self.delegate seg_CellClickAtIndexPath:_self.indexPath];
            }
        };
    }
    return  _contentLabel;
}

-(UIImageView *)titleImageView{
    if (!_titleImageView) {
        _titleImageView = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_opendoor_title")]];
        _titleImageView.hidden = YES;
    }
    return  _titleImageView;
}

-(UIButton *)submitButton{
    if (!_submitButton) {
        _submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitButton.hidden = YES;
        _submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_submitButton setTitleColor:kColor_T forState:UIControlStateNormal];
        [_submitButton setTitle:@"门禁报修" forState:UIControlStateNormal];
        _submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_submitButton sizeToFit];
        [self.contentView addSubview:_submitButton];
        __weak typeof(self) _self = self;
        [_submitButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(seg_doorFix)]) {
                [_self.delegate seg_doorFix];
            }
        }];
    }
    return  _submitButton;
}

-(UISwitch *)switchButton{
    if (!_switchButton) {
        _switchButton = [UISwitch new];
        _switchButton.hidden = YES;
        _switchButton.on = YES;
        _switchButton.onTintColor = kColor_T;
        //[_switchButton addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventTouchUpInside];
        [_switchButton addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged | UIControlEventTouchDragExit];
    }
    return  _switchButton;
}

-(void)switchClick:(UISwitch*)sender{
    __weak typeof(self) _self = self;
    __block BOOL isOn = sender.isOn;
    if (_self.delegate && [_self.delegate respondsToSelector:@selector(seg_CellSwitch:)]) {
        [_self.delegate seg_CellSwitch:isOn];
    }
}

-(void)seg_canOpenDoor:(BOOL)canOpen Event:(NSString *)event{
    if (self.delegate && [self.delegate respondsToSelector:@selector(seg_canOpenDoor:IndexPath: Event:)]) {
        [self.delegate seg_canOpenDoor:canOpen IndexPath:self.indexPath Event:event];
    }
}
@end
